<?php

/**
 * This is the model class for table "tbl_file".
 *
 * The followings are the available columns in table 'tbl_file':
 * @property integer $id
 * @property string $title
 * @property string $description
 * @property string $url_address
 * @property string $download_sum
 * @property integer $category_id
 * @property string $create_date
 * @property string $update_date
 */
class File extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return File the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_file';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'required', 'message'=>'文件名称不能为空'),
            array('url_address', 'required', 'message'=>'文件下载地址不能为空'),
            array('url_address', 'url', 'message'=>'文件下载地址格式错误'),
            array('category_id', 'required', 'message'=>'文件类型不能为空'),
			array('category_id', 'numerical', 'integerOnly'=>true),
			array('title, url_address', 'length', 'max'=>255),
			array('download_sum', 'length', 'max'=>20),
			array('description, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, description, url_address, download_sum, category_id, create_date, update_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'type'=>array(self::BELONGS_TO, 'Type', 'category_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => '文件名称',
			'description' => '资源描述',
			'url_address' => '资源地址',
			'download_sum' => '下载次数',
			'category_id' => '文件类型',
			'create_date' => '创建日期',
			'update_date' => '更新日期',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('url_address',$this->url_address,true);
		$criteria->compare('download_sum',$this->download_sum,true);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('update_date',$this->update_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    
    /**
	 * 行为操作
	 */
	public function behaviors(){
		return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'create_date',
                'updateAttribute' => 'update_date',
                'setUpdateOnCreate' => 'true',
            )
		);
	}
}